package com.qujia.customer.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.common.utils.StringUtils;
import com.qujia.customer.domain.StationReturnCashUse;
import com.qujia.customer.service.IStationReturnCashUseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Wrapper;
import java.util.List;

@RestController
@RequestMapping("/stationReturnCashUse")
public class StationReturnCashUseController {

    @Autowired
    private IStationReturnCashUseService stationReturnCashUseService;

    @GetMapping("getByStationId")
    public AjaxResult getByStationId(@RequestParam("stationId") Long stationId){
        if (stationId == null || stationId == 0){
            return AjaxResult.parameterException();
        }
        return AjaxResult.success(stationReturnCashUseService.list(new LambdaQueryWrapper<StationReturnCashUse>()
                .eq(StationReturnCashUse::getStationId,stationId)
                .orderByAsc(StationReturnCashUse::getConsumptionThreshold)));
    }

    @PostMapping("/addByStation")
    public AjaxResult addStationReturnCashUse(@RequestBody List<StationReturnCashUse> stationReturnCashUseList){
        for (StationReturnCashUse item : stationReturnCashUseList) {
            if (item.getStationId() == null || item.getStationId() == 0
                    || item.getConsumptionThreshold() == null || item.getUseAmount() == null){
                return AjaxResult.parameterException();
            }
        }
        stationReturnCashUseService.batchUpdateOrInsert(stationReturnCashUseList, SecurityUtils.getUsername());
        return AjaxResult.success();
    }

    @DeleteMapping("/del/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        if (stationReturnCashUseService.removeByID(id)){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

}
